package com.wangyadong.hobby.admin.controller;

import com.wangyadong.hobby.admin.servcie.IAnswerService;
import com.wangyadong.hobby.admin.servcie.IQuestionService;
import com.wangyadong.hobby.common.question.Answer;
import com.wangyadong.hobby.common.question.Question;
import com.wangyadong.hobby.common.utils.HobbyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController("/api/admin/question")
public class QuestionController {

    @Autowired
    private IAnswerService iAnswerService;
    @Autowired
    private IQuestionService iQuestionService;

    @PostMapping("/question")
    public HobbyResult addQuestion(Question question) throws Exception {
        int i = iQuestionService.saveOrUpdateQuestion(question);
        return HobbyResult.success();
    }

    @GetMapping("/detail")
    public HobbyResult getQuestionDetail(Long id) throws IllegalAccessException, InstantiationException {
        Question questionDetail = iQuestionService.getQuestionDetail(id);
        return HobbyResult.success(questionDetail);
    }

    @DeleteMapping("/delete")
    public HobbyResult delete(Long id) {
        return HobbyResult.success(iQuestionService.deleteQuestion(id));
    }

    @PostMapping("/list")
    public HobbyResult getQuestionList(int pageNum, int pageSize, Map<String, Object> map) throws IllegalAccessException, InstantiationException {
        return HobbyResult.success(iQuestionService.queryQuestionListByPage(pageNum, pageSize, map));
    }

    @PostMapping("/answer")
    public HobbyResult answer(Answer answer) throws Exception {
        return HobbyResult.success(iAnswerService.saveOrUpdateAnswer(answer));
    }

    @DeleteMapping("/deleteAnswer")
    public HobbyResult deleteAnswer(Long id) {
        return HobbyResult.success(iAnswerService.deleteAnswer(id));
    }

    @GetMapping("answerDetail")
    public HobbyResult HobbyDetail(Long id) throws IllegalAccessException, InstantiationException {
        return HobbyResult.success(iAnswerService.getAnswerDetail(id));
    }
}
